# This code is distributed under the terms of the GPL
#
# (c) Tiago Freitas Leal
# (c) Drew S. Dupont - Modifications
# Modified by Tiago Freitas Leal: 15.08.2004

$version = '2.3';

#	&easymod ($targetfile, $searchfile, $linestodelete, $insertfile [,$times])
#		- supports multiple lines in $searchfile
#		- $linestodelete counts from the first line of the matched multi-line string
#		- $times is optional and specifies how many time changes the set full of operations should be made:
#		  undefined (or zero) = only once / positive = that number of times / negative = until end of file
#
#	&installed ($file, $string1 [, $string2])
#		- search for $string1 (optionally search $string2 on the next line) into $file => 1 found / 0 not found
#
#	&search_replace ($targetfile, $searchfile, $replacefile [, $times])
#		- on the target file, replace the lines in $searchfile with the lines in $replacefile
#		- $times as above
#
#
#	$storebkp =	where to store backup of the files that are changed by your mod
#	$moddir =	where your mod files are
#	$wkdir =	where you are changing files (backup from and copy to)
#
#	backupinstall ($file ,$wkdir ,$storebkp ,$moddir);
#	backup ($file ,$wkdir ,$storebkp);
#	install ($file ,$wkdir ,$moddir);
#	uninstallrestore ($file ,$wkdir ,$storebkp);
#	uninstall ($file  ,$wkdir);

sub backupinstall
{
	my $file = $_[0];
	my $wkdir = $_[1];
	my $storebkp = $_[2];
	my $moddir = $_[3];
	print "Backing up $wkdir/$file to $storebkp/$file ...\n";
	system "/bin/cp -fp $wkdir/$file $storebkp/$file";
	
	print "Installing $moddir/$file to $wkdir/$file ...\n";
	system "/bin/rm -f $wkdir/$file";
	system "/bin/cp -p $moddir/$file $wkdir/$file";
}

sub backup
{
	my $file = $_[0];
	my $wkdir = $_[1];
	my $storebkp = $_[2];
	print "Backing up $wkdir/$file to $storebkp/$file ...\n";
	system "/bin/cp -fp $wkdir/$file $storebkp/$file";
}

sub install
{
	my $file = $_[0];
	my $wkdir = $_[1];
	my $moddir = $_[2];
	print "Installing $moddir/$file to $wkdir/$file ...\n";
	system "/bin/rm -f $wkdir/$file";
	system "/bin/cp -p $moddir/$file $wkdir/$file";
}

sub uninstallrestore
{
	my $file = $_[0];
	my $wkdir = $_[1];
	my $storebkp = $_[2];
	print "Uninstalling $wkdir/$file ...\n";
	system "/bin/rm -f $wkdir/$file";
	
	print "Restoring $storebkp/$file to $wkdir/$file ...\n";
	system "/bin/cp -p $storebkp/$file $wkdir/$file";
}

sub uninstall
{
	my $file = $_[0];
	my $wkdir = $_[1];
	print "Uninstalling $wkdir/$file ...\n";
	system "/bin/rm -f $wkdir/$file";
}

sub easymod
{
	my $targetfile = $_[0];
	my $searchfile = $_[1];
	my $linestodeleteorig = $_[2];
	my $insertfile = $_[3];
	my $times = $_[4];
	if(!$times || $times == 0)
	{
		$times = 1;
	}

	open(FILE, "$targetfile") or die 'Unable to open target file.';
	my @target = <FILE>;
	close(FILE);
	
	open(FILE, "$searchfile") or die 'Unable to open search file';
	my @search = <FILE>;
	close(FILE);
	
	open(FILE, "$insertfile") or die 'Unable to open insert file.';
	my @insert = <FILE>;
	close(FILE);
	
	open(FILE, ">/tmp/temp") or die 'Unable to open temporary file.';
	flock FILE, 2;

	print "Modifying $targetfile ...\n";

	my $line;
	my $otherline;
	my $linestodelete = 0;
	my $count = 0;
	my $searchcount = @search;
	my @store;

	foreach $line(@target)
	{
		if($line eq "$search[$count]" && $times != 0)
		{	# match and do it this time
			$store[$count] = $line; # store the line
			$count++;
			
			if($count == $searchcount)
			{   # FOUND
				$linestodelete = $linestodeleteorig;
				foreach $otherline(@store)
				{
					if($linestodelete > 0)
					{
						$linestodelete--;
					}
					else
					{
						print FILE "$otherline";			 # empty the storage
					}
				}
				
				$times--;
				@store = ();							 # clear the storage
				$count = 0;							 # reset search line counter
				foreach $otherline(@insert)
				{
					print FILE $otherline;
				}  # insert file
			}									 # END OF FOUND
			# if line sequence not found, do nothing else
		}
		else
		{									 # NO match
			if($count > 0)
			{							 # incomplete line sequence was found
				foreach $otherline(@store)
				{
					print FILE "$otherline";
				} # empty the storage
				
				@store = ();							 # clear the storage
				$count = 0;							 # reset search line counter
			}
			
			if($linestodelete > 0)
			{
				$linestodelete--; # deleting the remaining lines
			}
			else
			{
				print FILE "$line";
			}
		}
	}
	
	close(FILE);
	system "/bin/cp /tmp/temp $targetfile";
	system "/bin/rm -f /tmp/temp";
}

sub installed
{
	my @searchstring;
	my $targetfile = $_[0];
	$searchstring[0] = $_[1];
	$searchstring[1] = $_[2];

	open(FILE, "$targetfile") or die 'Unable to open target file.';
	my @target = <FILE>;
	close(FILE);

	my $line;
	my $count = 0;
	my $searchcount = 1;
	if($searchstring[1])
	{
		$searchcount = 2;
	}
	
	foreach $line(@target)
	{
		if($line eq "$searchstring[$count]\n")
		{
			$count++;
			if($count == $searchcount)
			{
				return 1;
			}
		} 
		else
		{
			if($count > 0) 
			{
				$count = 0;
			}
		}
	}
	
	return 0;
}

sub search_replace
{
	my $targetfile = $_[0];
	my $searchfile = $_[1];
	my $replacefile = $_[2];
	my $times = $_[3];
	if(!$times || $times == 0)
	{
		$times = 1;
	}

	open(FILE, "$searchfile") or die 'Unable to open search file';
	my @search = <FILE>;
	close(FILE);

	my $searchcount = @search;
	&easymod ($targetfile, $searchfile, $searchcount, $replacefile, $times)
}
